package com.leessy.r229.utils;

import java.util.UUID;

import android.app.ActivityManager;
import android.app.usage.StorageStatsManager;
import android.content.Context;
import android.os.Build;
import android.os.storage.StorageManager;

import androidx.annotation.RequiresApi;

import com.leessy.r229.App;
@RequiresApi(api = Build.VERSION_CODES.O)
public class MemoryStatus {
    static final int ERROR = -1;

    // 获取内部存储总大小
    public static long getTotalInternalStorageSize() {
        try {
            UUID uuid = StorageManager.UUID_DEFAULT;
            StorageStatsManager statsManager = (StorageStatsManager) App.app.getSystemService(Context.STORAGE_STATS_SERVICE);
            return statsManager.getTotalBytes(uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 获取手机内部可用空间大小
     *
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    static public long getAvailableInternalMemorySize() {
//        File path = Environment.getDataDirectory();
//        StatFs stat = new StatFs(path.getPath());
//        long blockSize = stat.getBlockSize();
//        long availableBlocks = stat.getAvailableBlocks();
//        return availableBlocks * blockSize;
        try {
            UUID uuid = StorageManager.UUID_DEFAULT;
            StorageStatsManager statsManager = (StorageStatsManager) App.app.getSystemService(Context.STORAGE_STATS_SERVICE);
            return statsManager.getFreeBytes(uuid);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
    // 获取总RAM大小（单位：字节）
    public static long getTotalRamSize() {
        ActivityManager activityManager = (ActivityManager) App.app.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);

        return memoryInfo.totalMem; // 总RAM大小
    }

    // 获取已用RAM大小（单位：字节）
    public static long getUsedRamSize() {
        ActivityManager activityManager = (ActivityManager) App.app.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);

        long totalRam = memoryInfo.totalMem;
        long availableRam = memoryInfo.availMem;

        return totalRam - availableRam; // 已用RAM大小
    }
    static public String formatSize(long size) {
        String suffix = null;

        if (size >= 1000) {
            suffix = "KiB";
            size /= 1000;
            if (size >= 1000) {
                suffix = "MiB";
                size /= 1000;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null)
            resultBuffer.append(suffix);
        return resultBuffer.toString();
    }
    static public String formatSizeRam(long size) {
        String suffix = null;

        if (size >= 1024) {
            suffix = "KiB";
            size /= 1024;
            if (size >= 1024) {
                suffix = "MiB";
                size /= 1024;
            }
        }

        StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
        int commaOffset = resultBuffer.length() - 3;
        while (commaOffset > 0) {
            resultBuffer.insert(commaOffset, ',');
            commaOffset -= 3;
        }

        if (suffix != null)
            resultBuffer.append(suffix);
        return resultBuffer.toString();
    }
}